package br.figmania.repositorio;

import br.figmania.conectation.HibernateUtility;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;


public class Dao {
	
	/*
	public Disciplina buscarObjeto(Disciplina disciplina){
		
		Session sessao = HibernateUtility.getSession();
		Transaction transacao = sessao.beginTransaction();
		
		try {
			
		Disciplina materia = (Disciplina) sessao.get(Disciplina.class,disciplina.getCodigo());
		transacao.commit();
		return materia;
		
		}catch (Exception e){
			e.printStackTrace();
			return null;
			
		}finally {
			sessao.close();
		}
		
	}
	
	*/
	public <T> boolean inserirObjeto(T objeto) {

		Session sessao = HibernateUtility.getSession();
		Transaction transacao = sessao.beginTransaction();
		try {
			sessao.save(objeto);
			transacao.commit();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			sessao.close();
		}
		
	}

	public <T> boolean atualizarObjeto(T objeto) {

		Session sessao = HibernateUtility.getSession();
		Transaction transacao = sessao.beginTransaction();
                transacao.begin();
		try {
			sessao.update(objeto);
			transacao.commit();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			sessao.close();
		}
		
	}

	public <T> boolean deletarObjeto(T objeto) {

		Session sessao = HibernateUtility.getSession();
		Transaction transacao = sessao.beginTransaction();
		try {
			sessao.delete(objeto);
			transacao.commit();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			sessao.close();
		}
		
	}

	public List<?> listarObjeto(String query) {
		
		Session sessao = HibernateUtility.getSession();
		//Transaction tx = sessao.beginTransaction();
		
		try {
			Query select = sessao.createQuery(query);
			List list = select.list();
		//	tx.commit();
			return list;
		} catch (Exception e) {
                    
                    e.printStackTrace();
			
			return null;
		} finally {
			sessao.close();
		}
		
	}
        
        public List listarProduto(String query){
        List usuarios;
        
        Session sessao = HibernateUtility.getSession();
        Transaction tx = sessao.beginTransaction();
        Query select = sessao.createQuery(query);
        usuarios = select.list();
        tx.commit();
        
        sessao.close();
        
        return usuarios;
    }
	
	public int getQuantidadeLinhas(String tabela){
		
		Session sessao = HibernateUtility.getSession();
		Query query = sessao.createQuery("from " + tabela);
		List <?> lista =  query.list();
		return lista.size();
		
	}
	
	public Object getUltimoID(String tabela){
		
		Session sessao = HibernateUtility.getSession();
		Query query = sessao.createQuery("select MAX(id) from " + tabela);
		List <?> lista = query.list();
		return lista.get(0);
		
	}
        
           
}